package com.fjwt.gz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fjwt.gz.core.exception.BizException;
import com.fjwt.gz.db.dto.SysQaTypeDTO;
import com.fjwt.gz.db.entity.SysQaEntity;
import com.fjwt.gz.db.entity.SysQaTypeEntity;
import com.fjwt.gz.service.mapper.SysQaMapper;
import com.fjwt.gz.service.mapper.SysQaTypeMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author shiyonghao
 * @since 2025-04-09
 */
@Slf4j
@Service
public class SysQaTypeService extends ServiceImpl<SysQaTypeMapper, SysQaTypeEntity> {


    @Autowired
    private SysQaMapper sysQaMapper;



    /**
     * pc新增QA类别
     *
     * @param sysQaTypeDTO
     * @return
     */
    public void add(SysQaTypeDTO sysQaTypeDTO) {
        if (sysQaTypeDTO.getTypeName().isEmpty() || sysQaTypeDTO.getSysType().isEmpty() || sysQaTypeDTO.getTypeSort() == null) {
            throw new BizException("参数有误");
        }
        SysQaTypeEntity sysQaTypeEntity = BeanUtil.copyProperties(sysQaTypeDTO, SysQaTypeEntity.class);
        log.info("新增QA类别:{}", sysQaTypeEntity);
        //判断类别是否已存在
        LambdaQueryWrapper<SysQaTypeEntity> gw = new LambdaQueryWrapper<>();
        gw.eq(SysQaTypeEntity::getTypeName, sysQaTypeEntity.getTypeName());
        gw.eq(SysQaTypeEntity::getSysType, sysQaTypeEntity.getSysType());
        SysQaTypeEntity result = this.baseMapper.selectOne(gw);
        if (result != null) {
            throw new BizException("QA类别已存在,请重新添加");
        }
        save(sysQaTypeEntity);
    }

    /**
     * pc修改QA类别
     *
     * @param sysQaTypeDTO
     * @return
     */
    public void update(SysQaTypeDTO sysQaTypeDTO) {
        if (sysQaTypeDTO.getSysQaTypeId() == null) {
            throw new BizException("QA类别id不能为空");
        }
        if (sysQaTypeDTO.getTypeName().isEmpty()||sysQaTypeDTO.getSysType().isEmpty()) {
            throw new BizException("参数有误");
        }
        SysQaTypeEntity sysQaTypeEntity = BeanUtil.copyProperties(sysQaTypeDTO, SysQaTypeEntity.class);
        log.info("修改QA类别:{}", sysQaTypeEntity);
        SysQaTypeEntity result = this.baseMapper.selectOne(SysQaTypeEntity.gw()
                .eq(SysQaTypeEntity::getTypeName, sysQaTypeEntity.getTypeName())
                .eq(SysQaTypeEntity::getSysType, sysQaTypeEntity.getSysType()));
        if (result != null) {
            throw new BizException("QA类别已存在,请重新修改");
        }
        updateById(sysQaTypeEntity);
    }

    /**
     * pc删除QA类别
     *
     * @param sysQaTypeDTO
     * @return
     */
    public void remove(SysQaTypeDTO sysQaTypeDTO) {
        if (sysQaTypeDTO.getSysQaTypeId() == null) {
            throw new BizException("QA类别id不能为空");
        }
        //判断QA类别是否配置QA内容,如果有QA内容则不能删除
        LambdaQueryWrapper<SysQaEntity> qw = SysQaEntity.gw().eq(SysQaEntity::getSysQaTypeId, sysQaTypeDTO.getSysQaTypeId());
        List<SysQaEntity> sysQaEntities = sysQaMapper.selectList(qw);
        if (!sysQaEntities.isEmpty()) {
            throw new BizException("QA类别已配置QA内容，不能删除");
        }
        removeById(sysQaTypeDTO.getSysQaTypeId());
    }
}
